package org.duang.core;

import java.lang.annotation.Annotation;

import org.duang.aop.Interceptor;

import com.esotericsoftware.reflectasm.MethodAccess;



public class ActionInvocation {

	
	private Controller controller;
	private Action action;
	private Interceptor[] inters;
	private MethodAccess actionMethod;
	private int index = 0;
	private static final Object[] NULL_ARGS = new Object[0];
	
	protected ActionInvocation() {
		
	}
	
	public ActionInvocation(Action action, Controller controller, MethodAccess actionMethod ) {
		this.action = action;
		this.controller = controller;
		this.actionMethod = actionMethod;
		this.inters = action.getInterceptors();
		
	}
	
	/**
	 * Invoke the action.
	 * 反射方式执行该方法
	 * @throws Throwable 
	 */
	public void invoke() throws RuntimeException {		
		if (inters!=null && index < inters.length){
			inters[index++].intercept(this);
		}else {
			actionMethod.invoke(controller, action.getMethod().getName(), NULL_ARGS);
		}
	}
	
	public String getActionKey(){
		return action.getActionKey();
	}
	
	public Annotation[] getAnnotations(){
		return action.getMethod().getDeclaredAnnotations();
	}
}
